//
//  PersonData.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/21.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "PersonDataCtry.h"
#import "DataInfoModel.h"
#import "DataInfolist2.h"
#import "AFNetworking.h"
#import "GRUserModel.h"//研究生个人资料
#import "GRUserManager.h"//资料管理类
#import "MJExtension.h"
#import "DataHeadView.h"//个人资料headview
#import "TeacherCell.h"//个人资料Cell
#define DataHeadViewID @"DataHeadView"
#define TeacherCellID @"TeacherCell"
@interface PersonDataCtry ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *DatainfoTable;
@property (nonatomic, strong) NSMutableArray *GraduateArry;
@property (nonatomic, strong) NSMutableArray *TeacherArry;
@property (nonatomic, strong) NSMutableArray *PersonDatainfo;
@property(nonatomic,strong)GRUserModel *model;

@end

@implementation PersonDataCtry

/**
 *  研究生数据的懒加载
 */
- (NSMutableArray *)GraduateArry
{
    if (_GraduateArry == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"GraduateList.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建模型对象
            DataInfoModel *tg = [DataInfoModel DataInfowithDict:dict];
            
            // 3.2.添加模型对象到数组中
            [tgArray addObject:tg];
        }
        
        // 4.赋值
        _GraduateArry = tgArray;
    }
    return _GraduateArry;
}
/**
 *  教师数据的懒加载
 */
- (NSMutableArray *)TeacherArry
{
    if (_TeacherArry == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"TeacherList.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建模型对象
            DataInfolist2 *tg = [DataInfolist2 DataInfolist2withDict:dict];
            
            // 3.2.添加模型对象到数组中
            [tgArray addObject:tg];
        }
        
        // 4.赋值
        _TeacherArry = tgArray;
    }
    return _TeacherArry;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [GRUserManager me];
    self.title = @"个人资料";
    
    [self setupTable];
    // Do any additional setup after loading the view.
}
//UItableView初始化
- (void)setupTable
{
    _DatainfoTable=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    _DatainfoTable.showsHorizontalScrollIndicator=NO;
    _DatainfoTable.showsVerticalScrollIndicator=NO;
    _DatainfoTable.dataSource=self;
    _DatainfoTable.delegate=self;
 
    [self.view addSubview: self.DatainfoTable];

    //加载cell
    [_DatainfoTable registerNib:[UINib nibWithNibName:@"TeacherCell" bundle:nil] forCellReuseIdentifier:TeacherCellID];
    //加载Head或者foot
    [_DatainfoTable registerNib:[UINib nibWithNibName:@"DataHeadView" bundle:nil] forHeaderFooterViewReuseIdentifier:DataHeadViewID];
 
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0){
        return self.GraduateArry.count;
        
    }else{
        
        return self.TeacherArry.count;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TeacherCell *cell = [tableView dequeueReusableCellWithIdentifier:TeacherCellID];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    if(indexPath.section==0){
        DataInfoModel *datainfo=_GraduateArry[indexPath.row];
        cell.Dataimage.image=[UIImage imageNamed:datainfo.image];
        cell.Datanametext.text=datainfo.listtext;
        switch (indexPath.row) {
            case 0:
                {
                   cell.Loadinfo.text=self.model.phone;
                    break;
                }
            case 1:
            {
                cell.Loadinfo.text=self.model.address;
                break;
            }
            case 2:
               {
                cell.Loadinfo.text=self.model.qnumber;
                break;
               }
            case 3:
               {
                cell.Loadinfo.text=self.model.wnumber;
                break;
                }
            case 4:
            {
                cell.Loadinfo.text=self.model.jieshu;
                break;
            }
            case 5:
            {
                cell.Loadinfo.text=self.model.college;
                break;
            }
            case 6:
            {
                cell.Loadinfo.text=self.model.moneyway;
                break;
            }
            case 7:
            {
                cell.Loadinfo.text=self.model.moneynumber;
                break;
            }
            case 8:
            {
                cell.Loadinfo.text=self.model.achievement;
                break;
            }
                
            default:{
               
            }
                break;
        }
        
        return cell;
    }else{
        DataInfolist2 *datainfo1=_TeacherArry[indexPath.row];
        cell.Dataimage.image=[UIImage imageNamed:datainfo1.image];
        cell.Datanametext.text=datainfo1.listtext;
        switch (indexPath.row) {
            case 0:
                {
                cell.Loadinfo.text=self.model.mumphone;
                    break;
                }
            case 1:
            {
                cell.Loadinfo.text=self.model.jname;
                break;
            }
            case 2:
            {
                  cell.Loadinfo.text=self.model.wnumber;
                break;
            }
            default:
                cell.Loadinfo.text=self.model.jphone;
                break;
        }
        
        
        return cell;
    }
    

  
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section==0){
         return 208;
        
    }else{
        
        return 40;
    }
   
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

//#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

if (section==1) {
    return nil;
}else{
    DataHeadView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:DataHeadViewID];
     header.autoresizingMask = UIViewAutoresizingNone;
    header.GradutenaLab.text=self.model.name;
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",ImageBaseURL,[[NSUserDefaults standardUserDefaults] objectForKey:@"grpic"]];
    [header.txImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"头像"]];
    return header;
   
}
   
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if( section==1){
         return @"     所属教务老师";
    }else{
        return nil;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (indexPath.row == 3) {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.jphone];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
            [self.view addSubview:callWebview];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
